package ReviewStack;

import java.util.Stack;

public class Stack2 {
    public Stack<Integer> s;
    public Stack<Integer> minStack;
    public Stack2(){
        s=new Stack<>();
        minStack=new Stack<>();
    }
    //入栈
    public void push(int val){
        s.push(val);
        if(minStack.isEmpty()){
            minStack.push(val);
        }else{
            if(s.peek()<=minStack.peek()){
                minStack.push(s.peek());
            }
        }
    }
    //出栈
    public void pop(){
        int val1=s.pop();
        System.out.println("主栈出栈元素为："+val1);
        if(val1==minStack.pop()){
        int val2= minStack.pop();
        System.out.println("辅助栈出栈元素为:"+val2);
        }
    }
    //获取栈顶元素
    public int top(){
        if(s.isEmpty()){
            System.out.println("栈空！");
            return -1;
        }
        return s.peek();
    }
    //获取最小值
    public int getMain(){
        if(minStack.isEmpty()){
            System.out.println("最小栈空！");
            return -1;
        }
        return minStack.peek();
    }
}
